if (typeof accedo === "undefined") {
	accedo = {};
}

accedo.Achievement = {
	ajaxFunction : function() {
		var xmlHttp = null;
		try {
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
				}
			}
		}
		return xmlHttp;
	},
	addScore : function(username, score) {
		// get the xmlHttpRequest Object
		var xmlReq = this.ajaxFunction();
		// open the connection to the server
		xmlReq.open("get", "../Memory/scoreServlet?time="
				+ new Date().getTime() + "&username=" + username + "&score="
				+ score, true);
		// send data
		xmlReq.send(null);
	},
	triggerHiScore : function() {
		// get the xmlHttpRequest Object
		var xmlReq = this.ajaxFunction();
		// open the connection to the server
		xmlReq.open("get", "../Memory/hiServlet?time=" + new Date().getTime(),
				true);
		// send data
		xmlReq.send(null);

		// get the response from the server
		xmlReq.onreadystatechange = function() {
			if (xmlReq.readyState == 4) {
				if (xmlReq.status == 200 || xmlReq.status == 304) {
					var serverDataObj = eval("(" + xmlReq.responseText + ")");

					var tableNode = document.createElement("table");
					tableNode.id = "hiscoreTableID";
					 tableNode.border = "1";
					tableNode.style.tableLayout = "fixed";

					// create cells of the hiscore table
					for ( var rowNum = 0; rowNum < serverDataObj.hiscore.length; rowNum++) {
						var trNode = tableNode.insertRow();

						var tdNode1 = trNode.insertCell();
						tdNode1.align = "right";
						tdNode1.width = 300;
						tdNode1.height = 40;
						tdNode1.innerHTML = "<td>"
								+ serverDataObj.hiscore[rowNum].score + "</td>";

						var tdNode2 = trNode.insertCell();
						tdNode2.align = "left";
						tdNode2.width = 300;
						tdNode2.height = 40;
						tdNode2.innerHTML = "<td>"
								+ serverDataObj.hiscore[rowNum].username
								+ "</td>";

					}

					var divNode = document.getElementById("hiscoreDivID");
					var divChildNode = divNode.childNodes[0];
					if (divChildNode != null) {
						divNode.removeChild(divChildNode);
					}
					divNode.appendChild(tableNode);
				}
			}
		};
		// setTimeout("accedo.Achievement.triggerHiScore()", 4000);
	},
	triggerCardRecord : function(username) {
		// get the xmlHttpRequest Object
		var xmlReq = this.ajaxFunction();
		// alert(username);
		// open the connection to the server
		// xmlReq.open("get","../Memory/cardServlet?time=" + new
		// Date().getTime() + "username=" + username,true);
		xmlReq.open("get", "../Memory/cardServlet?username=" + username, true);
		xmlReq.setRequestHeader("If-Modified-Since", "0");
		// send data
		xmlReq.send(null);
		// alert(username);
		// get the response from the server
		xmlReq.onreadystatechange = function() {
			if (xmlReq.readyState == 4) {
				if (xmlReq.status == 200 || xmlReq.status == 304) {
					var serverDataObj = eval("(" + xmlReq.responseText + ")");

					var tableNode = document.createElement("table");
					tableNode.id = "cardTableID";
					// tableNode.border = "1";
					tableNode.style.tableLayout = "fixed";

					// create cells of the hiscore table
					for ( var rowNum = 0; rowNum < serverDataObj.cards.length; rowNum++) {
						var trNode = tableNode.insertRow();

						var tdNode1 = trNode.insertCell();
						tdNode1.align = "left";
						tdNode1.width = 100;
						tdNode1.height = 20;
						tdNode1.innerHTML = "<td>X&nbsp"
								+ serverDataObj.cards[rowNum].card_num
								+ "</td>";

						var tdNode2 = trNode.insertCell();
						tdNode2.align = "right";
						tdNode2.width = 100;
						tdNode2.height = 20;
						tdNode2.innerHTML = "<td><img src='Images/"
								+ serverDataObj.cards[rowNum].card_sn
								+ ".png'></img></td>";

					}

					var divNode = document.getElementById("cardDivID");
					var divChildNode = divNode.childNodes[0];
					if (divChildNode != null) {
						divNode.removeChild(divChildNode);
					}
					divNode.appendChild(tableNode);
				}
			}
		};
	},
	triggerAchi : function(username, achi_sn) {
		// get the xmlHttpRequest Object
		var xmlReq = this.ajaxFunction();
		// open the connection to the server
		xmlReq.open("get", "../Memory/achiServlet?time=" + new Date().getTime()
				+ "&username=" + username + "&achi_sn=" + achi_sn, true);

		// send data
		xmlReq.send(null);

		// get the response from the server
		xmlReq.onreadystatechange = function() {
			if (xmlReq.readyState == 4) {
				if (xmlReq.status == 200 || xmlReq.status == 304) {
					var serverDataObj = eval("(" + xmlReq.responseText + ")");
					var tdNode = document.getElementById("showAchiID");
					tdNode.innerHTML = "<marquee scrollAmount='16' loop='1'><font class='rightTitle'>You Got One Card: </font><img src='Images/"
							+ serverDataObj.pid + ".png'></img></marquee>";
					triggerCardRecord(MemoryGame.Misc.username);
				}
			}
		};
	},

	showAchi : function(username) {
		// get the xmlHttpRequest Object
		var xmlReq = this.ajaxFunction();
		// open the connection to the server
		xmlReq.open("get", "../Memory/achiShowServlet?time="
				+ new Date().getTime() + "&username=" + username, true);

		// send data
		xmlReq.send(null);

		// get the response from the server
		xmlReq.onreadystatechange = function() {

			if (xmlReq.readyState == 4) {
				if (xmlReq.status == 200 || xmlReq.status == 304) {
//					alert(xmlReq.responseText);
					var serverDataObj = eval("(" + xmlReq.responseText + ")");

					var tableNode = document.createElement("table");
					tableNode.id = "achiDivTableID";
					tableNode.border = "1";
					tableNode.style.tableLayout = "fixed";

					// create cells of the hiscore table
					for ( var rowNum = 0; rowNum < serverDataObj.achis.length; rowNum++) {
						var trNode = tableNode.insertRow();
						
						var tdNode0 = trNode.insertCell();
						tdNode0.align = "center";
//						tdNode0.border = "1";
						tdNode0.width = 100;
						tdNode0.height = 100;
						if (serverDataObj.achis[rowNum].achi_state == 'A') {
							tdNode0.innerHTML = "<td><img src='Images/unlock.png'></img></td>";
						} else {
							tdNode0.innerHTML = "<td>&nbsp</td>";
						}

						var tdNode1 = trNode.insertCell();
						tdNode1.align = "center";
						tdNode1.width = 680;
						tdNode1.height = 100;
						tdNode1.innerHTML = "<td><font class='rightTitle'>"
								+ serverDataObj.achis[rowNum].achi_name + "</font></td>";

						var tdNode2 = trNode.insertCell();
						tdNode2.align = "center";
						tdNode2.width = 100;
						tdNode2.height = 100;
						tdNode2.innerHTML = "<td><img src='Images/tempAchi.png'></img></td>";

					}
					var divNode = document.getElementById("achiDivID");
					var divChildNode = divNode.childNodes[0];
					if (divChildNode != null) {
						divNode.removeChild(divChildNode);
					}
					divNode.appendChild(tableNode);
				}
			}
		};
	}
};
